package util;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

public class Resources {
    private static String BUNDLE_NAME;
    private static ResourceBundle RESOURCE_BUNDLE;
    private static Locale langue = Locale.getDefault();
    private static Map<String, Locale> possibleLanguages;

    static {
        setLangue(Locale.FRANCE);
        possibleLanguages = new HashMap<String, Locale>();
        possibleLanguages.put("English",Locale.UK);
        possibleLanguages.put("Français",Locale.FRANCE);
    }

    private Resources() {
    }

    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

    public static void setLangue(Locale langue) {
        Resources.langue = langue;
        BUNDLE_NAME = getPackageName() + "messages";
        RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, langue);
        
    }
    
    public static void setLangue(String locale) {
        setLangue(possibleLanguages.get(locale));
    }

    private static String getPackageName() {
        return langue.getLanguage() + ".";
    }

    private static String getResourceFolder() {
        return "/" + langue.getLanguage() + "/";
    }
    
    public static Set<String> getPossibleLanguages() {
        return possibleLanguages.keySet();
    }

    public static InputStream getBagInputStream() {
        return Resources.class.getResourceAsStream(getResourceFolder()
                + "bag.txt");
    }
    
    public static InputStream getDictionaryInputStream() {
        return Resources.class.getResourceAsStream(getResourceFolder()
                + "dic.txt");
    }
    
    public static String getString(String key, Object... parameters) {
        MessageFormat mf = new MessageFormat(getString(key));
        mf.setLocale(langue);
        return mf.format(parameters);
    }
}
